package com.tledu.controller;

import com.tledu.domain.User;
import com.tledu.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author cyrus
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping("")
    @ResponseBody
    public String index(String name, Integer age) {
        System.out.println("姓名：" + name);
        System.out.println("年龄：" + age);
        userService.print();
        return "success";
    }

    @RequestMapping("/params")
    @ResponseBody
    public String params(User user, @RequestParam(value = "username",required = true,defaultValue = "默认") String name) {
        System.out.println(name);
        System.out.println("姓名：" + user.getName());
        System.out.println("年龄：" + user.getAge());
        userService.print();
        return "success";
    }

    @RequestMapping("/arr")
    @ResponseBody
    public String arrParams(String[] arrs) {
        for (int i = 0; i < arrs.length; i++) {
            System.out.println(arrs[i]);
        }
        return "success";
    }

    @RequestMapping("/list")
    @ResponseBody
    public String arrList(@RequestParam List<String> ids) {
        ids.forEach(id -> System.out.println(id));
        return "success";
    }


    @RequestMapping("/postJson")
    @ResponseBody
    public String postJson(@RequestBody User user) {
        System.out.println(user);
        return "success";
    }

    @RequestMapping("/postJsonArr")
    @ResponseBody
    public String postJsonArr(@RequestBody List<User> list) {
        list.forEach(System.out::println);
        return "success";
    }
}
